package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

public class ConfigUtil 
{
	private static Properties properties;
	private static String root="";
	private static String currentPath="";
	private static String ext_File="config-ext.properties";
	private static String path="com/util/config.properties";
	public static SimpleDateFormat dateFormat_YMD=new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat dateFormat_YMD_HMS=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat dateFormat_YMD_HMS_MS=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	public static SimpleDateFormat dateFormat_YMDHMSMS=new SimpleDateFormat("yyyyMMddHHmmssSSS");
	private static boolean loadCover=false;
	static
	{
		properties=new Properties();
		try {
			properties.load(ConfigUtil.class.getClassLoader().getResourceAsStream(path));
			System.out.println("ConfigUtil static ..."+new File(path).getAbsolutePath());
		} catch (IOException e) 
		{
			System.out.println("ConfigUtil static load exception..."+new File(path).getAbsolutePath());
			e.printStackTrace();
		}
		root=properties.getProperty("root");
		currentPath=root+"/"+ext_File;
		if (properties.get("load.cover")!=null)
		{
			loadCover=Boolean.parseBoolean(properties.getProperty("load.cover"));
		}
		cover();
		Runtime.getRuntime().addShutdownHook(new Thread()
		{
			@Override
			public void run()
			{
				try {
					save();
				} catch (Exception e) {
					//e.printStackTrace();
				}
			}
		});
		
		//
		//TimeContainer.init();
		
	}
	public static String getAttribute(String key)
	{
		//System.out.println("ConfigUtil properties :"+properties);
		return properties.getProperty(key);
	}
	public static void setAttribute(String key,String value)
	{
		 properties.put(key,value);
	}
	
	public static Object getObject(Class c)
	{
		if (ConfigUtil.getAttribute(c.getName())!=null)
		{
			try {
				return Class.forName(ConfigUtil.getAttribute(c.getName())).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	public static Object getObject(String name)
	{
		if (ConfigUtil.getAttribute(name)!=null)
		{
			try {
				return Class.forName(ConfigUtil.getAttribute(name)).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	public static void reload()
	{
		try {
			properties.load(ConfigUtil.class.getClassLoader().getResourceAsStream(path));
			System.out.println("ConfigUtil static reload..."+new File(path).getAbsolutePath());
		} catch (IOException e) 
		{
			System.out.println("ConfigUtil static reload exception..."+new File(path).getAbsolutePath());
			e.printStackTrace();
		}
	}
	public static void save()
	{
		if (!loadCover) {
			return;
		}
		File rootFile=new File(root);
		if (rootFile.exists()==false)
		{
			rootFile.mkdirs();
		}
		File extFile=new File(currentPath);
		if (extFile.exists()==false)
		{
			try {
				extFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			properties.save(new FileOutputStream(currentPath), "gbk");
			System.out.println("ConfigUtil save ..."+currentPath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static void cover()
	{
		if (!loadCover) {
			return;
		}
		File dFile=new File(root);
		if (dFile.exists()==false)
		{
			return;
		}
		Properties cProperties=new Properties();
		File f=new File(currentPath);
		if (f.exists()==false)
		{
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			cProperties.load(new FileInputStream(currentPath));
			Iterator iterator=cProperties.keySet().iterator();
			String key="";
			while (iterator.hasNext()) 
			{
				key=iterator.next().toString();
				//if (properties.get(key)==null) {
					properties.put(key, cProperties.get(key));
				//}
				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("ConfigUtil cover ..."+currentPath);
	}
	public static String getCurrentTimeInMillis()
	{
		return ConfigUtil.dateFormat_YMDHMSMS.format(new Date());
	}
	public static Properties getProperties() {
		return properties;
	}
}
